//
//  WZVisitorLoginView.swift
//  weiBo---01
//
//  Created by 王—wagn on 16/4/7.
//  Copyright © 2016年 王—wagn. All rights reserved.
//

import UIKit
import SnapKit

//两种写法,一种是必须实现代理,一种是选择实现,写选择实现,避免程序报错
@objc protocol WZVisitorLoginViewDelegate: NSObjectProtocol {
    
    optional func uesrWillLogin ()
    optional func uesrWillRegister ()
}



class WZVisitorLoginView: UIView {

    //声明代理
    weak var visitorDelegate:WZVisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //给外界提供方法,调动设置界面 设计到home界面,需要判断
    func setUpViewController(title:String,imageName:String,isHome:Bool) {
        tipLabel.text = title;
        circleIcon.image = UIImage(named: imageName)
        
        if isHome {
            
            startAnimation()
            
        }else {
        
            houseIcon.hidden = true
            
            bringSubviewToFront(circleIcon)
            
        }
    
    }
    
    
    
    //MARK: 设置约束,更新UI界面
    private func setUpUI() {
    
        addSubview(circleIcon)
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        addSubview(houseIcon)
        houseIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
//            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        addSubview(tipLabel)
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleIcon.snp_bottom).offset(20)
            make.width.equalTo(220)
            make.height.equalTo(40)
            make.left.equalTo(circleIcon.snp_left)
        }
    
        addSubview(loginBtn)
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom)
            make.width.equalTo(100)
            
        }
        
        addSubview(registerBtn)
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(loginBtn.snp_top)
            make.width.equalTo(loginBtn.snp_width)
            
        }
        
        addSubview(bacgGroudImage)
        bacgGroudImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }

        let value :(CGFloat) = 237/255.0
        //修改背景颜色
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //增加按钮点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    
    }
    
    //MARK:按钮点击事件
    @objc private func loginBtnDidClick() {
    
        print("登陆")
        //添加 ? 代表是否有值判读执行,不能添加! 不能强解,不一定执行
        visitorDelegate?.uesrWillLogin?()
    }
    @objc private func registerBtnDidClick() {
        print("注册")
        visitorDelegate?.uesrWillRegister?()
    
    }
    
    //MARK: 设置旋转动画
    private func startAnimation() {
    
        //创建基本动画
        let baseAnimation = CABasicAnimation(keyPath: "transform.rotation")
        //旋转时间
        baseAnimation.duration = 15
        //旋转次数
        baseAnimation.repeatCount = MAXFLOAT
        //旋转角度
        baseAnimation.toValue = 2 * M_PI
        // 动画执行结束 或者 当前处于非活跃状态下 动画不移除图层 防止切换页面后旋转效果消失
        baseAnimation.removedOnCompletion = false
        //增加旋转对象
        circleIcon.layer.addAnimation(baseAnimation, forKey: nil)
    
    
    }
    
    private lazy var bacgGroudImage : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //懒加载圆圈
    private lazy var circleIcon :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //懒加载房子
     private lazy var houseIcon :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //懒加载文字
    private lazy var tipLabel: UILabel = {
        
        let tip = UILabel()
        tip.text = "关注一些人,回到这里看看有什么惊喜"
        tip.textColor = UIColor.darkGrayColor()
        tip.font = UIFont.systemFontOfSize(14)
        tip.numberOfLines = 0
        tip.textAlignment = .Center
        tip.sizeToFit()
        return tip
    }()
    
    //加载按钮
    private lazy var registerBtn: UIButton = {
    
        let register = UIButton()
        register.setTitle("注册", forState: .Normal)
        register.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        register.titleLabel?.font = UIFont.systemFontOfSize(12)
        register.titleLabel?.sizeToFit()
        return register
    
    }()

    //加载按钮
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.titleLabel?.sizeToFit()
        return btn
        
    }()

    
}
